package com.main.antenne;

import java.util.ArrayList;

import ErgebnisFilterung.DatenFilter;
import ErgebnisFilterung.TabellenEintrag;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;

public class TabellenActivity extends Activity {

	private ArrayList<TabellenEintrag> tabellenListe;
	private LinearLayout basisLayout;
	private DownloadTask task;
	private ProgressDialog dialog;
	private ListView tabelle;
	private LinearLayout titelZeile;
	private AlertDialog alert;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabellen_layout);

		basisLayout = (LinearLayout) findViewById(R.layout.tabellen_layout);

		titelZeile = (LinearLayout) findViewById(R.id.tabellen_titel);
		titelZeile.setOrientation(LinearLayout.HORIZONTAL);

		titelZeile
				.setBackgroundResource(R.drawable.tabellenheader_grau_farbverlauf);

		tabelle = (ListView) findViewById(R.id.tabelle);

		// Dialog erstellen
		dialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_DARK);
		dialog.setMessage("Laden...");
		dialog.setCancelable(false);

		task = (DownloadTask) getLastNonConfigurationInstance();

		// Wenn noch kein Task gestartet ist, starte neuen Download-Task;
		// sonst
		// ersetze Activity des laufenden Tasks
		if (task == null) {
			task = new DownloadTask(this);
			task.execute();
		} else {
			task.setActivity(this);
			updateTabelle();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		// Falls die Android Version tiefer als HoneyComb liegt, wird die
		// standard-Titelzeile unsichtbar gemacht
		// sonst werden der Titelzeile die entsprechenden Eintr�ge je nach
		// Bildschirmdrehung hinzugef�gt
		if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
			if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"P", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.1)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Verein", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.45)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Sp.", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.15)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"+/-", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.15)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Pkt", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.15)));

			} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"P", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Verein", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.42)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Sp.", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"S", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"U", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"N", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"T", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"GT", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"+/-", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.06)));
				titelZeile.addView(new TableTextView(titelZeile.getContext(),
						"Pkt", true, Math.round(MainActivity
								.getWindowWidth(TabellenActivity.this)
								* (float) 0.1)));
			}
		} else
			titelZeile.setVisibility(TextView.INVISIBLE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MainActivity.addLiveStreamMenuItem(menu, this);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MainActivity.updateLiveStreamMenuItemText(menu.getItem(0));
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected void onStop() {
		// Bevor die Activity gestoppt wird, z.B. bei einer Bildschirmdrehung,
		// wird der Ladedialog oder der Fehlermeldungsdialog beendet, um eine
		// Exception zu vermeiden
		if (dialog != null)
			dismissDialog();
		if (alert != null && alert.isShowing())
			alert.dismiss();
		super.onStop();
	}

	/*
	 * anzeigen des Ladedialogs
	 */
	void showDialog() {
		dialog.show();
	}

	/*
	 * beenden des Ladedialogs
	 */
	void dismissDialog() {
		dialog.dismiss();
	}

	/*
	 * der aktuelle Adapter wird auf den aktualisierten Adapder aus dem
	 * Download-Task gesetzt
	 */
	void updateTabelle() {
		this.tabelle.setAdapter(task.adapter);
	}

	@Override
	@Deprecated
	public Object onRetainNonConfigurationInstance() {
		task.activityNull();
		return task;
	}

	// DOWNLOAD TASK
	public class DownloadTask extends AsyncTask<Void, Void, Void> {
		TabellenActivity activity;
		TableLayout tabelle;
		ListAdapter adapter;

		public DownloadTask(TabellenActivity activity) {
			this.activity = activity;
		}

		@Override
		public void onPreExecute() {
			activity.showDialog();
		}

		@Override
		public Void doInBackground(Void... unused) {
			DatenFilter filter = new DatenFilter(activity, "tabelle");
			filter.start();

			try {
				filter.getZuAktualisieren().acquire();
			} catch (InterruptedException e) {
			}
			tabellenListe = filter.getTabellenListe();

			return null;
		}

		@Override
		public void onPostExecute(Void unused) {
			this.adapter = new ListAdapter() {

				@Override
				public void unregisterDataSetObserver(DataSetObserver observer) {
					// TODO Auto-generated method stub

				}

				@Override
				public void registerDataSetObserver(DataSetObserver observer) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean isEmpty() {
					return false;
				}

				@Override
				public boolean hasStableIds() {
					// TODO Auto-generated method stub
					return false;
				}

				@Override
				public int getViewTypeCount() {
					return 1;
				}

				@Override
				public View getView(int position, View convertView,
						ViewGroup parent) {
					if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
						// Bei Versionen unter HoneyComb ist die TitelZeile in
						// das ListView integriert und kann somit mitgescrollt
						// werden
						// Bei allen anderen Versionen ist die TitelZeile fest
						// an der oberen Bildschirmseite, damit die Attribute
						// komfortabel abgelesen werden k�nnen
						if (position == 0) {
							LinearLayout row = new LinearLayout(activity);
							row.setOrientation(LinearLayout.HORIZONTAL);
							row.setMinimumHeight(60);
							row.setGravity(Gravity.CENTER);
							row.setBackgroundResource(R.drawable.tabellenheader_grau_farbverlauf);

							if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
								row.addView(new TableTextView(
										row.getContext(),
										"P",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.1)));
								row.addView(new TableTextView(
										row.getContext(),
										"Verein",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.45)));
								row.addView(new TableTextView(
										row.getContext(),
										"Sp.",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));
								row.addView(new TableTextView(
										row.getContext(),
										"+/-",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));
								row.addView(new TableTextView(
										row.getContext(),
										"Pkt",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));

							} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
								row.addView(new TableTextView(
										row.getContext(),
										"P",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"Verein",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.42)));
								row.addView(new TableTextView(
										row.getContext(),
										"Sp.",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"S",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"U",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"N",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"T",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"GT",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"+/-",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										"Pkt",
										true,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.1)));
							}
							return row;
						} else {
							LinearLayout row = new LinearLayout(activity);
							row.setOrientation(LinearLayout.HORIZONTAL);
							row.setMinimumHeight(60);
							row.setGravity(Gravity.CENTER);

							if (position % 2 != 0)
								row.setBackgroundResource(R.drawable.dunkelgelb_listitem_background);
							else
								row.setBackgroundResource(R.drawable.hellgelb_listitem_background);

							TabellenEintrag t = tabellenListe.get(position - 1);

							boolean isTitle = false;
							if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

								row.addView(new TableTextView(
										row.getContext(),
										t.getPosition(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.1)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getTeam(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.45)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getSpiele(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getTordifferenz(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getPunkte(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.15)));

								return row;
							} else if (activity.getResources()
									.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

								row.addView(new TableTextView(
										row.getContext(),
										t.getPosition(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getTeam(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.42)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getSpiele(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getSiege(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getUnentschieden(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getNiederlagen(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getTore(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getGegentore(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getTordifferenz(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.06)));
								row.addView(new TableTextView(
										row.getContext(),
										t.getPunkte(),
										isTitle,
										Math.round(MainActivity
												.getWindowWidth(TabellenActivity.this)
												* (float) 0.1)));

								return row;
							}
						}
					} else {
						LinearLayout row = new LinearLayout(activity);
						row.setOrientation(LinearLayout.HORIZONTAL);
						row.setMinimumHeight(60);
						row.setGravity(Gravity.CENTER);

						if (position % 2 == 0)
							row.setBackgroundResource(R.drawable.dunkelgelb_listitem_background);
						else
							row.setBackgroundResource(R.drawable.hellgelb_listitem_background);

						TabellenEintrag t = tabellenListe.get(position);

						boolean isTitle = false;
						if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

							row.addView(new TableTextView(
									row.getContext(),
									t.getPosition(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.1)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getTeam(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.45)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getSpiele(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.15)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getTordifferenz(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.15)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getPunkte(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.15)));

							return row;
						} else if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

							row.addView(new TableTextView(
									row.getContext(),
									t.getPosition(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getTeam(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.42)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getSpiele(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getSiege(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getUnentschieden(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getNiederlagen(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getTore(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getGegentore(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getTordifferenz(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.06)));
							row.addView(new TableTextView(
									row.getContext(),
									t.getPunkte(),
									isTitle,
									Math.round(MainActivity
											.getWindowWidth(TabellenActivity.this)
											* (float) 0.1)));

							return row;
						}
					}
					return null;
				}

				@Override
				public int getItemViewType(int position) {
					return 0;
				}

				@Override
				public long getItemId(int position) {
					return position;
				}

				@Override
				public Object getItem(int position) {
					return tabellenListe.get(position);
				}

				@Override
				public int getCount() {
					if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
						return tabellenListe.size() + 1;
					else
						return tabellenListe.size();
				}

				@Override
				public boolean isEnabled(int position) {
					return false;
				}

				@Override
				public boolean areAllItemsEnabled() {
					return false;
				}
			};

			activity.updateTabelle();
			activity.dismissDialog();
		}

		void activityNull() {
			activity = null;
		}

		void setActivity(TabellenActivity activity) {
			this.activity = activity;
		}

	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.reopenactivity,
				R.anim.activitydisappear);
	}

}
